function FitCurWeather(){
	var imgDim;
	if (prefs.getBool("showAddInfo") || prefs.getBool("showDescription")){
		imgDim = (2*prefs.getInt("rsH") < prefs.getInt("rsW")) ? prefs.getInt("rsH") : prefs.getInt("rsW")/2;
	}else{
		imgDim = (prefs.getInt("rsH") < prefs.getInt("rsW")) ? prefs.getInt("rsH") : prefs.getInt("rsW");
	}
	$("#icon, #outer").height(imgDim);
	$("#wind, #humidity, #city").width(prefs.getInt('rsW') - imgDim);
	$("#icon, #outer").width(imgDim);
	$("#city").css('font-size', imgDim/10);
	$("#temp").css("font-size", imgDim/3);

	$("#info").css('left', imgDim);
	$("#info").width(prefs.getInt("rsW") - imgDim);
	$("#wind, #humidity").css('font-size', imgDim/10);
}
function Fit3Days(){
	var imgDim;
	if (prefs.getBool("showAddInfo") || prefs.getBool("showDescription")){
		var koef = 1;
		if (prefs.getBool("showAddInfo")) koef += 0.4;
		if (prefs.getBool("showDescription")) koef += 0.2;
		//imgDim = (3*prefs.getInt("rsH") < koef*prefs.getInt("rsW")) ? prefs.getInt("rsH")/koef : prefs.getInt("rsW")/3;
		imgDim = (3*prefs.getInt("rsH") < 2*prefs.getInt("rsW")) ? prefs.getInt("rsH")/2 : prefs.getInt("rsW")/3;

		$("#outer").height(imgDim*2);
		$("#info").width(prefs.getInt("rsW"));
		$("#city").css('font-size', imgDim*0.2);
		$("#wind, #humidity").css('font-size', imgDim*0.2);
		if ($("#info").outerHeight() > imgDim)
			$("#city").css('font-size', imgDim*0.15);
	}else{
		imgDim = (3*prefs.getInt("rsH") < prefs.getInt("rsW")) ? prefs.getInt("rsH") : prefs.getInt("rsW")/3;
		$("#outer").height(imgDim);
	}
	$("#outer, #dayscont").width(imgDim*3);
	
	$(".days_icon, .day_box").width(imgDim);
	$(".days_icon, .day_box").height(imgDim);
	$(".days_low, .days_high, .days_dow").css('font-size', imgDim/5);

	var i;
	for (i = 1; i < 5; i++){
		$("#day"+parseInt(i)).css('left', (i-1)*imgDim);
	}
}
function FitCur3Days(){
	var imgDim = (2.5*prefs.getInt("rsH") < prefs.getInt("rsW")) ? prefs.getInt("rsH") : prefs.getInt("rsW")*0.4;
	
	$("#icon, #outer").height(imgDim);
	$("#icon, #outer").width(imgDim);
	$("#dayscont").css('left', imgDim);
	$("#city").css('font-size', imgDim/10);
	$("#temp").css("font-size", imgDim/3);
	$("#dayscont").width(imgDim*1.5);

	$("#info").css('left', imgDim);
	$("#info").width(prefs.getInt("rsW") - imgDim);
	$("#wind, #humidity").css('font-size', imgDim/10);

	$(".days_icon, .day_box").width(imgDim/2);
	$(".days_icon, .day_box").height(imgDim/2);
	$(".days_low, .days_high, .days_dow").css('font-size', imgDim/10);

	var i;
	for (i = 1; i < 5; i++){
		$("#day"+parseInt(i)).css('left', (i-1)*imgDim/2);
	}
}
function FitProcessor(){
	switch(prefs.getInt('layout')){
		case 0:
			FitCurWeather();
			break;
		case 1:
			Fit3Days();
			break;
		case 2:
			FitCur3Days();
			break;
	}
}

function setupFonts(){
	var i;
	var bColor = prefs.getString('back_color');
	if (bColor)
		$("#wrapper").css("background-color", bColor);
	
	var fntFamily = prefs.getString('fntFamily');
	$("#temp").css("font-family", fntFamily);
	var fntSize = prefs.getInt('fntSize');
	if (fntSize > 0)
		$("#temp").css("font-size", parseInt(fntSize) + "px");
	
	fntFamily = prefs.getString('cityFntFamily');
	$("#city").css("font-family", fntFamily);
	fntSize = prefs.getInt('cityFntSize');
	if (fntSize > 0)
		$("#city").css("font-size", parseInt(fntSize) + "px");

	fntFamily = prefs.getString('oFntFamily');
	$("#humidity, #wind").css("font-family", fntFamily);
	fntSize = prefs.getInt('oFntSize');
	if (fntSize > 0)
		$("#humidity, #wind").css("font-size", parseInt(fntSize) + "px");

	fntFamily = prefs.getString('ftFntFamily');
	$(".days_low, .days_high, .days_dow").css("font-family", fntFamily);
	fntSize = prefs.getInt('ftFntSize');
	if (fntSize > 0)
		$(".days_low, .days_high").css("font-size", parseInt(fntSize) + "px");
	fntSize = prefs.getInt('dowFntSize');
	if (fntSize > 0)
		$(".days_dow").css("font-size", parseInt(fntSize) + "px");
	
	var fntColor = prefs.getString("t_color");
	if (fntColor != "")
		$("#temp").css("color", fntColor);
	fntColor = prefs.getString("city_color");
	if (fntColor != "")
		$("#city").css("color", fntColor);
	fntColor = prefs.getString("o_color");
	if (fntColor != "")
		$("#humidity, #wind").css("color", fntColor);
	fntColor = prefs.getString("ft_color");
	if (fntColor != "")
		$(".days_low, .days_high").css("color", fntColor);
	fntColor = prefs.getString("dow_color");
	if (fntColor != "")
		$(".days_dow").css("color", fntColor);
	
	if (prefs.getInt('layout') == 0)
		$("#wind, #humidity, #city").width(prefs.getInt('rsW') - parseInt($("#icon").width()));
}

function firstCharToUpper(str){
	var fch = str.substr(0, 1);
	return fch.toUpperCase() + str.substr(1);
}

function ApplyWeather(fore_infoObj, cur_condObj, fore_condArr){
    var i;
    var units = prefs.getInt("unit");
    var temp = (units == 0) ? cur_condObj.temp_c : cur_condObj.temp_f;
    var units_str = (units == 0) ? "°C" : "°F";
    
    applyElementHtml("temp", temp + units_str);
    
    if (cur_condObj.icon !== null && cur_condObj.icon != "") {
	applyElementAttribute("icon", 'src', convertIcon(cur_condObj.icon));
	applyElementAttribute("icon", 'title', cur_condObj.condition);
    }
    else {
	$("#icon").hide();
    }
    
    if (prefs.getBool("showAddInfo")){
	applyElementHtml("humidity", cur_condObj.humidity);
	applyElementHtml("wind", cur_condObj.wind_condition);
    }
    
    if (prefs.getBool("showDescription")){
	applyElementHtml("city", firstCharToUpper(fore_infoObj.postal_code));//postal_code more accurate then city
    }
    
    if (prefs.getInt('layout') == 1 && !prefs.getBool('fit')){
	var baseH = 64;
	
	if (prefs.getBool("showAddInfo")){
	    if (!prefs.getBool("showDescription")){
		$("#humidity").css("top", 0);
		$("#wind").css("top", 18);
	    }
	    
	    baseH += 37;
	}
	
	if (prefs.getBool("showDescription")) baseH += 19;
	
	$("#outer").css("height", baseH);
    }
    
    if (prefs.getInt('layout') > 0) {
	for (i = 0; i < 4; i++) {
	    if (fore_condArr[i].icon !== null && fore_condArr[i].icon != "") {
		applyElementAttribute("day" + parseInt(i + 1) + "_icon", 'src', convertIcon(fore_condArr[i].icon));
		applyElementAttribute("day" + parseInt(i + 1) + "_icon", 'title', fore_condArr[i].condition);
	    }
	    else {
		$("#day" + parseInt(i + 1) + "_icon").hide();
	    }
	    
	    applyElementHtml("day"+parseInt(i+1)+"_low", convertUnits(fore_condArr[i].low)+"°");
	    applyElementHtml("day"+parseInt(i+1)+"_high", convertUnits(fore_condArr[i].high)+"°");
	    applyElementHtml("day"+parseInt(i+1)+"_dow", firstCharToUpper(fore_condArr[i].day_of_week));
	}
    }
    
    setupFonts();
}